package cn.fjssj.demo02.controller;

import cn.fjssj.demo02.base.BaseImportExcelVO;
import cn.fjssj.demo02.base.ServerResponse;
import cn.fjssj.demo02.domain.PandaEntity;
import cn.fjssj.demo02.enums.ResponseCode;
import cn.fjssj.demo02.exception.BusinessException;
import cn.fjssj.demo02.model.dto.PandaQueryDTO;
import cn.fjssj.demo02.model.dto.PandaStatisticsDTO;
import cn.fjssj.demo02.service.PandaService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

// https://juejin.cn/post/7405158771017220131#heading-3
// https://juejin.cn/post/7406482061731479571?searchId=202411201434245AC398588F39C67387D9
@RestController
@RequestMapping("panda")
public class PandaController {
    @Resource
    private PandaService pandaService;

    /**
     * 我们正常读取了excel数据，并将读到的数据行批量保存到了数据库里
     *
     * @param file
     * @return
     */
    @PostMapping("import/v1")
    public ServerResponse<Void> importExcelV1(MultipartFile file) {
        if (null == file) {
            throw new BusinessException(ResponseCode.FILE_IS_NULL);
        }
        pandaService.importExcelV1(file);
        return ServerResponse.success();
    }

    /**
     * 根据搜索条件导出数据的例子
     *
     * @param queryDTO
     * @param response
     */
    @PostMapping("export/v1")
    public void exportExcelV1(@RequestBody PandaQueryDTO queryDTO, HttpServletResponse response) {
        pandaService.exportExcelByCondition(queryDTO, response);
    }

    /**
     * 校验导入的Excel数据
     *
     * @param file MultipartFile
     * @return
     */
    @PostMapping("import/v2")
    public ServerResponse<BaseImportExcelVO<String>> importExcelV2(MultipartFile file) {
        if (null == file) {
            throw new BusinessException(ResponseCode.FILE_IS_NULL);
        }
        return ServerResponse.success(pandaService.importExcelV2(file));
    }

    /**
     * 部分字段不同的导出场景
     *
     * @param statisticsDTO
     * @param response
     */
    @PostMapping("export/v3")
    public void exportExcelV3(@RequestBody PandaStatisticsDTO statisticsDTO, HttpServletResponse response) {
        pandaService.exportStatisticsData(statisticsDTO, response);
    }

    @PostMapping("export/v4")
    public void exportExcelV4(HttpServletResponse response) {
        pandaService.exportMultiLineHeadExcel(response);
    }

    @PostMapping("fill/v1")
    public void fillExcelV1(@RequestBody PandaEntity panda, HttpServletResponse response) {
        pandaService.fillExcelV1(panda, response);
    }

    @GetMapping("fill/v2")
    public void fillExcelV2(HttpServletResponse response) {
        pandaService.fillExcelV2(response);
    }

    /**
     * 批量填充后在导出
     *
     * @param num
     * @param name
     * @param response
     */
    @GetMapping("fill/v3/{num}/{name}")
    public void fillExcelV3(@PathVariable("num") Integer num, @PathVariable("name") String name, HttpServletResponse response) {
        pandaService.fillExcelV3(num, name, response);
    }

    // 如果数据量较大，可以分批填充
    @GetMapping("fill/v4/{num}/{name}")
    public void fillExcelV4(@PathVariable("num") Integer num, @PathVariable("name") String name, HttpServletResponse response) {
        pandaService.fillExcelV4(num, name, response);
    }

    // 混合式填充场景
    @GetMapping("fill/v5/{num}/{name}")
    public void fillExcelV5(@PathVariable("num") Integer num, @PathVariable("name") String name, HttpServletResponse response) {
        pandaService.mixedFill(num, name, response);
    }

    // 列表横向填充场景
    @GetMapping("fill/v6/{num}/{name}")
    public void fillExcelV6(@PathVariable("num") Integer num, @PathVariable("name") String name, HttpServletResponse response) {
        pandaService.transverseListFill(num, name, response);
    }

    // 多列表数据填充场景
    /*
    掌握列表数据怎么横向填充后，现在来想一个问题，列表占位符是通过{.xxx}这种形式声明，那如果一个报表中，存在多个列表需要填充，这该怎么办？
    如果两个集合都有name字段，这时该填充到哪儿去？对于这种场景，如果再依靠前面那种占位符，显然会导致数据乱套。
    EasyExcel为了解决这类填充场景，推出了新的占位符写法：{xx.xxx}，也就是前面再新增一个变量名，来看模板定义：
    * */
    @GetMapping("fill/v7/{num}/{name}")
    public void fillExcelV7(@PathVariable("num") Integer num, @PathVariable("name") String name, HttpServletResponse response) {
        pandaService.manyListFill(num, name, response);
    }

    /**
     * 复杂报表导出实战
     *
     * @param response HttpServletResponse
     */
    @PostMapping("/export/v7")
    public void exportExcelV7(HttpServletResponse response) {
        pandaService.exportPandaStatisticsData(response);
    }

    /**
     * 百万级导出优化实战
     * @return
     */
    @PostMapping("/export/v6")
    public ServerResponse<Long> exportExcelV6() {
        return ServerResponse.success(pandaService.export1mPandaExcelV2());
    }

}
